package com.example.demo.interceptor;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;

import com.example.demo.utils.ResultInfo;
//拦截器

public class Login_Interceptor implements HandlerInterceptor {

	private final static String outlogin = "{\"status\":-100}";// json字符chuan

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		String url = request.getRequestURI();

		if (request.getSession().getAttribute("user") == null) {
			if (url.indexOf(".html") > 0) {
				// response.sendRedirect("/login.html");
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().write(
						"<script type=\"text/javascript\">if(parent)parent.location.href='/login.html';</script>");

			} else {

				response.setContentType("text/html;charset=utf-8");
				response.getWriter().write(outlogin);
			}
			return false;
		}
		System.out.println(request.getRequestURL());
		return HandlerInterceptor.super.preHandle(request, response, handler);

	}

}
